package com.graduation.smartparkingplatform.Controller;

import com.github.pagehelper.PageInfo;
import com.graduation.smartparkingplatform.Pojo.PayingRecord;
import com.graduation.smartparkingplatform.Response.Response;
import com.graduation.smartparkingplatform.Response.ResponseUtils;
import com.graduation.smartparkingplatform.Service.PayingRecordService;
import com.graduation.smartparkingplatform.Utils.ExcelUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/payingRecord")
public class PayingRecordController {

  @Autowired private PayingRecordService payingRecordService;
  // 获取支付记录列表
  @GetMapping("/payingRecordList")
  public Response getpayingRecordList(
      @RequestParam(value = "page", defaultValue = "1") Integer page,
      @RequestParam(value = "count", defaultValue = "10") Integer count) {
    try {
      PageInfo<PayingRecord> getpayingRecordList =
          payingRecordService.getpayingRecordList(page, count);
      return ResponseUtils.buildSuccess(getpayingRecordList);
    } catch (Exception e) {
      return ResponseUtils.buildError(e);
    }
  }

  // 模糊搜索
  @GetMapping("/searchPayingRecord")
  public Response searchPayingRecord(
      @RequestParam(value = "page", defaultValue = "1") Integer page,
      @RequestParam(value = "count", defaultValue = "10") Integer count,
      @RequestParam(value = "outTradeNo", defaultValue = "null") String outTradeNo,
      @RequestParam(value = "payStatus", defaultValue = "null") String payStatus) {
    try {
      PageInfo<PayingRecord> searchList =
          payingRecordService.searchPayingRecord(page, count, outTradeNo, payStatus);
      return ResponseUtils.buildSuccess(searchList);
    } catch (Exception e) {
      return ResponseUtils.buildError(e);
    }
  }

  // 删除支付记录
  @PostMapping("/deletePayingRecord")
  public Response deletePayingRecord(@RequestParam("PayId") Integer PayId) {
    Boolean delete = payingRecordService.deletePayingRecord(PayId);
    if (delete) {
      return ResponseUtils.buildSuccess("删除支付记录成功");
    }
    return ResponseUtils.buildError("删除支付记录失败");
  }

  // 导出支付记录Excel
  @RequestMapping("/exportPayingRecordExcel")
  public void exportDistinguishExcel(HttpServletResponse response) {

    List<PayingRecord> recordList = payingRecordService.getPayingRecordListAll();

    // 获取当前时间
    Calendar calendar = Calendar.getInstance();
    // 定义时间格式化
    SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd_HH:mm:ss");
    String nowTime = formatter.format(calendar.getTime());

    // 导出操作
    ExcelUtils.exportExcel(
        recordList,
        "支付记录",
        "sheet1",
        PayingRecord.class,
        "PayingRecordDATA" + nowTime + ".xls",
        response);
    log.info("exportPayingRecordExcel");
  }
}
